<?php

/**
 * Flickr Photosets - Photoset Page
 * Displays a single photoset.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 8.5.22
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets and Flickr
*/
$fp = new FlickrPhotosetsCanvas();

// If the user has disabled photosets
if (!$fp->config['enable_gallery_photosets']) {
	if ($fp->config['enable_gallery_photostream']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/');
	}
	else if ($fp->config['enable_gallery_geotagged']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/geo/');
	}
	else {
		$fp->error(FlickrPhotosetsErrors::$photosets_disabled);
	}
}

// Require that we have the set id passed in the url
$fp->require_variables($_GET['sid']);

// Get the photoset and the photos in the set
$photoset = $fp->flickr->photosets_getInfo($_GET['sid']);
$photos = $fp->flickr->photosets_getPhotos($photoset['id'], 'media');

// Make sure the NSID matches that of the photoset
if ($fp->nsid != $photoset['owner']) {
	$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/sets/');
}

// Remove photos that do not match the privacy settings
$photos['photo'] = $fp->remove_photos($photos['photo']);

// Get the total number of photos
$photo_count = count($photos['photo']);

// Get page information
$page_count = ceil($photo_count / $fp->photos_per_page);
$first_photo = 1 + ($fp->photos_per_page * ($fp->page - 1));
$last_photo = $first_photo + ($fp->photos_per_page - 1);
$last_photo = ($last_photo > $photo_count) ? $photo_count : $last_photo;

// Slice the array to fit the page
$photos['photo'] = array_slice($photos['photo'], $first_photo-1, $fp->photos_per_page);

// Figure out the page that this set is on
$photosets = $fp->flickr->photosets_getList($fp->nsid);
$key = Utilities::multiArraySearch($photoset['id'], $photosets['photoset']);
$photosets_page = ceil(($key[0] + 1) / $fp->sets_per_page);

?>

<script src="<?=$fp->callback_url?>/scripts/common.js?3" type="text/javascript"></script>
<script type="text/javascript">
	// Context
	var callbackUrl = "<?=$fp->callback_url?>";
	var uid = "<?=$fp->uid?>";
	var sid = "<?=$photoset['id']?>";

	// Map stuff
	var mapHolder = $('photo_map');
	var photo = $('photo_album');
	var mapLink = $('maplink');
	var heightElement = photo;
</script>
<?php /* <script src="<?=$fp->callback_url?>/scripts/map.js?3" type="text/javascript"></script> */ ?>
<script type="text/javascript">
<?php include 'scripts/map.js'; ?>
</script>

<fb:title><?=$fp->get_name_fbml(true)?> Photosets | <?=$photoset['title']?></fb:title>
<?=$fp->get_stylesheet('photoset')?>

<?=$fp->get_header('photosets', $photoset['title'])?>

<div id="photo_header" class="clearfix">
	<div class="summary">
		<?php if ($photo_count > 0) { ?>
			Showing <?=$first_photo?> to <?=$last_photo?> of <?=$photo_count?> photos
			<span class="pipe">|</span>
		<?php } ?>
		<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/page/<?=$photosets_page?>/">Back to <?=$fp->get_name_fbml()?> photosets</a>
	</div>	
	<?=Pager::getPager($fp->page, $page_count, $fp->canvas_url.'/photos/'.$fp->uid.'/sets/'.$photoset['id'].'/')?>
</div>

<?php if ($photo_count > 0) { ?>
<div class="photo_grid">
	<div id="photo_map" style="opacity: 0; display: none; background-image: url(<?=$fp->callback_url?>/images/ajax-loader-large.gif);">&nbsp;</div>
	
	<table>
		<tr>
			<?php $i = 0;
			foreach ($photos['photo'] as $photo): ?>
				<td>
					<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$photo['id']?>/">
						<div class="thumb">
						<?php if ($photo['media'] == 'video') { ?>
							<img src="http://l.yimg.com/g/images/video_play_icon_medium.png.v1" class="video_icon" />
						<?php } ?>
						<fb:user-agent includes="ie 6">
							<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'thumbnail')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
						</fb:user-agent>
						<fb:user-agent excludes="ie 6">
							<img src="<?=$fp->flickr->buildPhotoUrl($photo, 'small')?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
						</fb:user-agent>
						</div>
					</a>
				</td>
			<?php $i++;
			echo ($i % 5 == 0) ? '</tr><tr>' : ''; ?>
			<?php endforeach; ?>
		</tr>
	</table>	
</div>
<?php 
}
else {
	$fp->error(FlickrPhotosetsErrors::$photosets_no_photos);
}
?>

<?php if ($photo_count > 0) { ?>
	
	<div id="photo_footer_bar" class="bar clearfix footer_bar">
		<?=Pager::getPager($fp->page, $page_count, $fp->canvas_url.'/photos/'.$fp->uid.'/sets/'.$photoset['id'].'/')?>
	</div>
	
	<div id="photo_footer">
		
		<div id="side">
			<fb:share-button class="meta">
				<meta name="title" content="Flickr Photoset: <?=$photoset['title']?>" />
				<meta name="description" content="<?=htmlentities($photoset['description'])?>" />
				<link rel="image_src" href="<?=$fp->flickr->buildPhotoUrl($fp->flickr->photos_getInfo($photoset['primary']), 'thumbnail')?>" />
				<link rel="target_url" href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/" />
			</fb:share-button>
			
			<div id="photo_actions">
				<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$photoset['id']?>/">Flickr Photoset Page</a>
				<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$photoset['id']?>/comments/">Flickr Comments</a>
				<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$photoset['id']?>/map/">Flickr Map</a>
				<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$photoset['id']?>/show/">Flickr Slideshow</a>
			</div>
		</div>
		
		<ul id="photo_info">
			<li id="description"><?=str_replace("\n", '<br />', $photoset['description'])?></li>
			<li id="slideshow">See a <a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/show/">slideshow</a> of this photoset.</li>
			<!--<li id="map">See these photos <a id="maplink" href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$photoset['id']?>/map/">on a map</a>.</li>-->
		</ul>
		
		<div id="photo_comments">
			<?php if ($fp->config['enable_comments']) { ?>
				<div class="facebook">
					<fb:comments xid="flickrphotosetsphotoset_<?=$photoset['id']?>" canpost="true" candelete="<?=($fp->uid == $fp->viewer_uid) ? 'true' : 'false' ?>" showform="true" send_notification_uid="<?=$fp->uid?>">
						<fb:title>Comments on <?=$photoset['title']?></fb:title>
					</fb:comments>
				</div>
			<?php } ?>
			<div class="flickr">
				<?=$fp->print_comments($fp->flickr->photosets_comments_getList($photoset['id']))?>
				<div class="clearfix"></div>
			</div>
		</div>
		
	</div>
<?php } ?>

<div class="clearfix"></div>

<?=$fp->echo_footer(true)?>